package com.woniuxy.web.log;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * 日志输出方式
 */
@Slf4j
@Aspect
@Component
public class LogAdvice {

    /**
     * 定义LogInfo注解日志输出内容
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("@annotation(com.woniuxy.web.log.LogInfo)")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature ms = (MethodSignature)pjp.getSignature();
        LogInfo logInfo = ms.getMethod().getDeclaredAnnotation(LogInfo.class);
        log.info(logInfo+"111111111111111111");
        log.info(ms.getName());
        log.info("=================开始执行{}-{}=================",logInfo.value(),ms.getName());
        if (logInfo.param()) log.info("参数为：{}", JSONUtil.parseArray(pjp.getArgs()).toString());
        Long start = null;
        if (logInfo.time()) start = System.currentTimeMillis();
        Object value = pjp.proceed();
        if (start==null){
            log.info("================={}-{}执行结束=================",logInfo.value(),ms.getName());
        }else {
            log.info("================={}-{}执行结束，耗时:{}=================",logInfo.value(),ms.getName(),System.currentTimeMillis()-start);
        }
        if (logInfo.result()) log.info("结果为：{}",JSONUtil.parse(value).toString());
        return value;
    }
}
